<!-- 新建通知 -->
<script setup>
import { ref, reactive } from 'vue'
import { useCourseStore } from '@/stores/index'
import { TeaNotice } from '@/api/course'
import { useRouter } from 'vue-router'
import { toRefs } from '@vueuse/core'
const courseStore = useCourseStore()
const router = useRouter()

const form = reactive({
  Title: '',
  ClassId: '',
  NetAddress: ''
})

// 网页
const file = ref(false)
const fileFrom = ref({
  title: '',
  address: ''
})
const tianjia = (e) => {
  e.preventDefault()
  file.value = !file.value
}
const addWangye = () => {
  const { address } = toRefs(fileFrom)
  console.log(address.value)
  form.NetAddress = address.value
  file.value = !file.value
}

form.teacherId = JSON.parse(localStorage.getItem('userInfo')).teacherId
//将得到的东西放在变量中
const noticeData = ref([])
// 提交
const onsubmit = () => {
  TeaNotice(form).then((res) => {
    console.log(res)
    if (res.data.code == 1) {
      console.log(res.data.user)
      noticeData.value = res.data.user
      console.log(noticeData.value)
      //将数据存储到本地
      localStorage.setItem('noticeData', JSON.stringify(noticeData.value))
      console.log(localStorage.getItem('noticeData'))
      // 跳转到通知页面

      router.push('/coursedetail/notice')
    }
  })
}
</script>

<template>
  <el-card class="my-card">
    <el-form :model="form" class="myform" label-width="auto">
      <el-form-item>
        <div>新建通知</div>
      </el-form-item>
      <el-form-item label="题目">
        <el-input v-model="form.Title" />
      </el-form-item>
      <el-form-item label="收件人">
        <el-select v-model="form.ClassId" placeholder="选择收件对象">
          <el-option value="全体学生">全体学生</el-option>
          <el-option
            v-for="item in courseStore.allClass"
            :key="item.classId"
            :label="item.classId"
            :value="item.classId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="正文">
        <el-input v-model="form.NetAddress" type="textarea" />
        &nbsp;&nbsp;
        <button type="button" @click="tianjia"><WangYe></WangYe></button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onsubmit">发布</el-button>
        <el-button @click="router.replace('/coursedetail/notice')">返回</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 上传文件 -->
  <el-dialog v-model="file" title="添加超链接" width="700px">
    <el-form :model="fileFrom" style="max-width: 600px" label-width="auto" class="demo-dynamic">
      <el-form-item label="网址">
        <el-input v-model="fileFrom.address" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addWangye">确定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<style lang="scss">
.my-card {
  margin: 0 auto;
  width: 1100px;

  .myform {
    margin: 0 auto;
    padding: 20px;
    border-radius: 6px;
  }
}
</style>
